from rest_framework import serializers
from django.shortcuts import reverse

from .. import models


class FileSerializer(serializers.ModelSerializer):
    link = serializers.SerializerMethodField()
    owner = serializers.SlugRelatedField(
        read_only=True,
        slug_field='username'
    )

    class Meta:
        model = models.File
        exclude = ['file', 'object_content_type', 'object_id', 'allowed_users']

    def get_link(self, file: models.File):
        return reverse('media:raw', kwargs={'pk': file.uuid})
